/**   
 * @Title: NewsAct.java 
 * @Package com.esun.cheetah.view.news
 * @Description: 新闻
 * @author 孔祥攀 <br/>
 * @date 2012-4-26 下午5:51:29
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.cheetah.view.news;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;

import com.esun.cheetah.R;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.network.ConnectionChangeReceiver;

/**
 * 
 * @Package com.esun.cheetah.view.news.NewsAct
 * @ClassName: NewsAct
 * @Description: TODO(新闻)
 * @author 孔祥攀 <br/>
 * @date 2012-4-26 下午5:51:29
 * 
 */
public class NewsAct extends ActivityGroup implements OnClickListener,
		OnCheckedChangeListener {
	private ProgressDialog progressDialog;

	private TabHost newsTabHost;
	private ProgressBar pbRef;
	private RadioButton huoD;
	private RadioButton lotteryNews;
	private Button news_agp_btn_refresh;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.news);

		newsTabHost = (TabHost) findViewById(R.id.news_agp_tabhost);
		newsTabHost.setup(NewsAct.this.getLocalActivityManager());

		huoD = (RadioButton) findViewById(R.id.news_agp_rb_huod);
		huoD.setOnClickListener(this);

		lotteryNews = (RadioButton) findViewById(R.id.news_agp_rb_lottrynews);
		lotteryNews.setOnClickListener(this);

		news_agp_btn_refresh = (Button) findViewById(R.id.news_agp_btn_refresh);
		news_agp_btn_refresh.setOnClickListener(this);

		// news_agp_ll_refresh = (LinearLayout)
		// findViewById(R.id.news_agp_ll_refresh);

		// 全部新闻标签 彩票资讯
		TabSpec newsRecordTS = newsTabHost.newTabSpec("LOTTRYNEWS_NEWS");
		newsRecordTS.setIndicator("彩票资讯");
		newsRecordTS.setContent(new Intent(this, LotteryNewsAct.class));
		newsTabHost.addTab(newsRecordTS);

		// 全部新闻标签 活动专区
		TabSpec huodRecordTS = newsTabHost.newTabSpec("HUOD_NEWS");
		huodRecordTS.setIndicator("活动专区");
		huodRecordTS.setContent(new Intent(this, HuoDAct.class));
		newsTabHost.addTab(huodRecordTS);

		// progressDialog = ViewerUtil.showProgressDialog(NewsAct.this,
		// getResources().getString(R.string.loading_text));
		// progressDialog.show();
		Constant.bar = progressDialog;

		newsTabHost.setCurrentTab(0);

		pbRef = (ProgressBar) findViewById(R.id.news_agp_pb_refresh);
		pbRef.setOnClickListener(this);

		final TabWidget tab_widget = newsTabHost.getTabWidget();
		int width = 45;
		int height = 50;

		for (int i = 0; i < tab_widget.getChildCount(); i++) {
			/**
			 * 设置高度、宽度，不过宽度由于设置为fill_parent，在此对它没效果
			 */
			tab_widget.getChildAt(i).getLayoutParams().height = height;
			tab_widget.getChildAt(i).getLayoutParams().width = width;
		}
	}

	public static boolean isCanClick = true;

	@Override
	public void onClick(View v) {

		Message msg = new Message();
		msg.what = Config.DO_REFRESH;

		switch (v.getId()) {
		case R.id.news_agp_rb_lottrynews:
			if (isCanClick) {
				newsTabHost.setCurrentTab(0);
				news_agp_btn_refresh.setVisibility(View.VISIBLE);
				LotteryNewsAct.handlerNews.sendMessage(msg);
				isCanClick = false;
			}

			break;

		case R.id.news_agp_rb_huod:
			if (isCanClick) {
				newsTabHost.setCurrentTab(1);
				news_agp_btn_refresh.setVisibility(View.VISIBLE);
				HuoDAct.handlerNews.sendMessage(msg);
				isCanClick = false;
			}

			break;

		case R.id.news_agp_btn_refresh:

			news_agp_btn_refresh.setVisibility(View.VISIBLE);

			int current = newsTabHost.getCurrentTab();
			if (current == 0) {
				lotteryNews.performClick();
			} else if (current == 1) {
				huoD.performClick();
			}

			break;

		default:

			break;

		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Activity parent = getParent();
			if (parent != null) {
				parent.onKeyDown(keyCode, event);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver
				.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.registerReceiver(
				ConnectionChangeReceiver.getConnectionChangeReceiver(),
				ConnectionChangeReceiver.getIntentFilter());
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
	}
}
